package edu.base.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import edu.base.util.HigherResponse;
import edu.base.entity.vo.RecordOrderVO;
import edu.base.service.RecOrderService;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * @author 10173
 *
 */
@Controller		
@RequestMapping("/order")
public class RecOrderCon {
	
	@Autowired
	private RecOrderService recOrderService;
	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;
	
//	@RequestMapping("/order")	
//	public String advance() {		
//		return "/kefu/records/rec-order";
//	}
	
	//添加客户确认单
	@ResponseBody
	@RequestMapping("/recOrder.do")
	public HigherResponse<String> addRecOrder(Integer repId, String userName, String userMobilephone) {
		return recOrderService.addRecOrder(repId, userName, userMobilephone);
	}
	
	//打印客户确认单
	@ResponseBody
	@RequestMapping("/printOrder.do")
	public HigherResponse<RecordOrderVO> queryOrder() {
		return recOrderService.queryOrder();
	}
	
	//客户确认单打印 fremarker生成HTML页面
	@ResponseBody
	@RequestMapping("/print.do")
	public String print() throws Exception{
		Configuration configuration=freeMarkerConfigurer.getConfiguration();
		// 加载模板对象
		Template template = configuration.getTemplate("recOrder.ftl");
		// 创建一个数据集
		Map<String, Object> data = new HashMap<>();
		// 向数据集中添加需要的所有数据
		RecordOrderVO recOrder=recOrderService.queryOrder2();
		data.put("recordordervo", recOrder);
		// 指定文件输出路径以及文件名Writer out = new FileWriter(new File("F:/Temp/freemarker/recOrder.html"));
		Writer out = new FileWriter(new File("E:/Workspace/Java/JavaEE/eclipse-workspace-2019R/emms/src/main/resources/public/printOrder.html"));
		// 输出文件
		template.process(data, out);
		// 关闭流
		out.close();
		return "done";
	}
}
